package jpf.nextsearch.util.validation.validator;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jpf.nextsearch.util.validation.base.GenericValidator;

/**
 * @author <a href="mailto:jacek.pruszkowski@gmail.com">Jacek Pruszkowski</a>
 */
public class DateValidator extends GenericValidator {
	
	private static final String DATE_RULE = "\\d\\d-\\d\\d-\\d\\d\\d\\d";
	private Pattern datePattern = Pattern.compile(DATE_RULE);
	
	@Override
	protected boolean isValueValidInternal(Object value) {
		String date = (String) value;
		Matcher matcher = datePattern.matcher(date);
		if (matcher.matches()) {
			DateFormat dateFormat = new SimpleDateFormat(COMMON_DATE_FORMAT);
			try {
				dateFormat.parse(date);
				return true;
			} catch (ParseException e) {
				return false;
			}
		}
		return false;
	}
	
	@Override
	protected boolean supports(Class<?> klass) {
		return klass.isAssignableFrom(String.class);
	}
}
